package com.xjx.service.impl;

import com.xjx.dao.ProviderDao;
import com.xjx.pojo.PageInfo;
import com.xjx.pojo.Provider;
import com.xjx.service.ProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProviderServiceImpl implements ProviderService {
    @Autowired
    private ProviderDao providerDao;
//
    @Override
    public PageInfo<Provider> findByPage(int currentPage, int size, String proName, String proCode, String startDate,
                                         String endDate) {
        PageInfo<Provider> pageInfo = new PageInfo<>();
        pageInfo.setSize(size);
        //获取总数据量
        int totalCount = providerDao.count(proName,proCode,startDate,endDate);//中断
        pageInfo.setTotalCount(totalCount);
        int totalPage =  (int)Math.ceil(totalCount/(double)pageInfo.getSize());
        pageInfo.setTotalPage(totalPage);
        if(currentPage<1){
            pageInfo.setCurrentPage(1);
        }else if(currentPage > totalPage){
            pageInfo.setCurrentPage(totalPage);
        }else {
            pageInfo.setCurrentPage(currentPage);
        }
        int start = (pageInfo.getCurrentPage()-1)*pageInfo.getSize();
        if (start<0){
            start=0;
        }
        List<Provider> list = providerDao.findByPage(start, pageInfo.getSize(),proName, proCode,startDate,endDate);
        pageInfo.setList(list);
        return pageInfo;
    }

    @Override
    public int save(Provider provider) {
        return providerDao.save(provider);
    }

    @Override
    public Provider findById(Integer id) {
        return providerDao.findById(id);
    }

    @Override
    public int modify(Provider provider) {
        return providerDao.modify(provider);
    }

    @Override
    public int delete(Integer id) {
        return providerDao.delete(id);
    }


}
